package com.farmer.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSClientBuilder;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

public class Upload {
//    阿里域名
    public static final String ALI_DOMAIN="https://pgh1118.oss-cn-chengdu.aliyuncs.com/";
    public static String uploadImage(MultipartFile file) throws IOException {
        //    生成文件名
        String originalFilename = file.getOriginalFilename();//原来的图片名
        String ext = "."+ FilenameUtils.getExtension(originalFilename);
        String uuid = UUID.randomUUID().toString().replace("-","");
        String fileName = uuid+ext;
//    地域节点
        String endpoint="https://oss-cn-chengdu.aliyuncs.com";
        String accessKeyId = "LTAI5tQVgA7RmRaRwSKQBbD4";
        String accessKeySecret = "rOEgRdaMwNp3dxNywv3tSjulXldBCq";

//    OSS客户端对象
        OSS ossClient = new OSSClientBuilder().build(endpoint,accessKeyId,accessKeySecret);
        ossClient.putObject(
         "pgh1118",//仓库名
         fileName,
         file.getInputStream()
        );
        ossClient.shutdown();
        return ALI_DOMAIN+fileName;
    }

}
